package tools;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class RequestIdGenerator implements CircularSeqGenerator {

    private static final RequestIdGenerator INSTANCE = new RequestIdGenerator();
    private static final short SEQ_UPPER_LIMIT = 999;
    private short sequence = -1;

    private RequestIdGenerator() {
    }

    @Override
    public short nextSequence() {
        if (sequence >= SEQ_UPPER_LIMIT) {
            sequence = 0;
        } else {
            sequence++;
        }
        return sequence;
    }

    public String nextId() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyMMddHHmmss");
        String timestamp = simpleDateFormat.format(new Date());
        DecimalFormat decimalFormat = new DecimalFormat("0000");
        short sequenceNo = nextSequence();
        return "0049" + timestamp + decimalFormat.format(sequenceNo);
    }


    public static RequestIdGenerator getInstance() {
        return INSTANCE;
    }
}
